package cn.exrick.xboot.modules.CheckConn.service.impl;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.jiuqiCommon.callAPIUtil.CallSMSservice;
import cn.exrick.xboot.modules.CheckConn.entity.CheckConn;
import cn.exrick.xboot.modules.ReceiveSMS.entity.ReceiveSMS;
import cn.exrick.xboot.modules.ReceiveSMS.service.IReceiveSMSService;
import cn.exrick.xboot.modules.callApi.entity.SMS;
import cn.exrick.xboot.modules.system.entity.System;
import cn.exrick.xboot.modules.system.service.ISystemService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: njp
 * @Date: 2020/05/28/11:29
 * @Description:
 */
@Service
@Slf4j
public class CheckConnImpl {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ISystemService iSystemService;
    @Autowired
    private IReceiveSMSService iReceiveSMSService;
    @Autowired
    private CallSMSservice callSMSservice;
    private  CheckConn checkConn;


    public String getRdisvalueByKey(CheckConn checkConn){
        this.checkConn=checkConn;
        String value=null;
        value = redisTemplate.opsForValue().get(CommonConstant.checkConn + checkConn.getSender());
        if(StrUtil.isBlank(value)){
            //如果redis中没有对应的值说明连接异常
            boolean b = updateCheckConnStatus();
            if(b){
                //发送预警短信
               Map<String,Object>  sysmap=new HashMap<>();
                sysmap.put("module_name",checkConn.getSender());
                Collection<System> systems = iSystemService.listByMap(sysmap);
                for (System system : systems) {
                    if("start".equals(system.getWarningStatus())){
                        Map<String,Object> sendsms=new HashMap();
                        sendsms.put("sys_id",system.getId());
                        Collection<ReceiveSMS> receiveSMS = iReceiveSMSService.listByMap(sendsms);
                        for (ReceiveSMS receiveSM : receiveSMS) {
                            //发送短信
                            log.info("发送"+checkConn.getSender()+"的短信预警");
                            SMS sms=new SMS();
                            sms.setDESTADDR(receiveSM.getTelephone());
                            sms.setMESSAGECONTENT(checkConn.getSender()+"连接异常");
                            boolean b1 = callSMSservice.sendSMS(sms);
                            
                        }
                    }
                }
            }
        }
        return "";
    }

    public  boolean  updateCheckConnStatus(){
        System system = new System();
        //将状态设置为异常
        system.setConnStatus("2");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("module_name",this.checkConn.getSender());
        boolean update = iSystemService.update(system, queryWrapper);
        log.info(update?"修改服务状态成功":"修改服务状态失败");
        return update;
    }

}
